﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            //添加3个类，分别实现 IComparer<T>接口，实现对Student类的三个字段的排序。
            //1、学生类：学号、姓名、年龄
            //2、请选择：1、添加学生信息。2、删除学生信息 2、查询学生信息。
            //3、重复的学号不能添加。
            //4、查询学生信息功能中有：1、查询所有（按学号排序）2、查询所有（按姓名排序），
            //2、查询所有（按年龄排序）4、按学号查询（查没有，则打印查无此学生）5、退出

            Student s1 = new Student(1, "傻逼1", 20);
            Student s2 = new Student(3, "傻逼3", 21);
            Student s3 = new Student(2, "傻逼2", 22);
            Student s4 = new Student(4, "傻逼4", 23);
            Student s5 = new Student(5, "傻逼5" ,24);                   
            List<Student> stu = new List<Student> { s1,s2,s3,s4,s5 };
            Dictionary<int, Student> student = new Dictionary<int, Student>();
            student.Add(s1.Num, s1);
            student.Add(s2.Num, s2);
            student.Add(s3.Num, s3);
            student.Add(s4.Num, s4);
            student.Add(s5.Num, s5);
            
            text(ref stu, ref student);
        }

        private static void text(ref List<Student> stu, ref Dictionary<int, Student> student)
        {
            Console.WriteLine("1、添加学生信息。2、删除学生信息 3、查询学生信息");
            int choose1 = int.Parse(Console.ReadLine());
            switch (choose1)
            {
                case 1:
                    addstudent(ref stu, ref student);
                    text(ref stu, ref student);
                    break;
                case 2:
                    removestu(ref stu, ref student);
                    text(ref stu, ref student);
                    break;
                case 3:
                    inquirestu(ref stu, ref student);
                    text(ref stu, ref student);
                    break;
                default:
                    text(ref stu, ref student);
                    break;
            }
        }

        private static void inquirestu(ref List<Student> stu, ref Dictionary<int, Student> student)
        {
            Console.WriteLine("1、查询所有（按学号排序）2、查询所有（按姓名排序），3、查询所有（按年龄排序）4、按学号查询（查没有，则打印查无此学生）5、退出");
            int choose2 = int.Parse(Console.ReadLine());
            switch (choose2)
            {
                case 1:
                    
                    foreach (var i in stu)
                    {
                        Console.WriteLine(i.ToString());
                    }
                    text(ref stu, ref student);
                    break;
                case 2:
                    stu.Sort(new Numsort());
                    foreach (var i in stu)
                    {
                        Console.WriteLine(i.ToString());
                    }
                    text(ref stu, ref student);
                    break;
                case 3:
                    stu.Sort(new Agesort());
                    foreach (var i in stu)
                    {
                        Console.WriteLine(i.ToString());
                    }
                    text(ref stu, ref student);
                    break;
                case 4:
                    stu.Sort(new Numsort());
                    foreach (var i in stu)
                    {
                        Console.WriteLine(i.ToString());
                    }
                    text(ref stu, ref student);

                    break;
                case 5:

                    break;
                default:
                    break;
            }
        }

        private static void removestu(ref List<Student> stu, ref Dictionary<int, Student> student)
        {
            Console.WriteLine("请输入想要删除学生的学号");
            int num = int.Parse(Console.ReadLine());
            
           
            if (student.ContainsKey(num))
            {
                
                student.Remove(num);
                Console.WriteLine("删除成功");
            }
            else
            {
                Console.WriteLine("没有找到该学生");
                Console.WriteLine("删除失败");
                text(ref stu, ref student);
            }
           
        }

        private static void addstudent(ref List<Student> stu,ref Dictionary<int, Student> student)
        {
            Console.WriteLine("请输入学号：");
            int num = int.Parse(Console.ReadLine());
            Console.WriteLine("请输入姓名：");
            string name = Console.ReadLine();
            Console.WriteLine("请输入年龄");
            int age = int.Parse(Console.ReadLine());
            Student a = new Student(num, name, age);
            if (student.ContainsKey(num))
            {
                Console.WriteLine("该学号已存在！！");
                Console.WriteLine("添加失败");
                addstudent(ref stu,ref student);
                
            }
            else
            {
                stu.Add(a);
                student.Add(a.Num,a);
                Console.WriteLine("添加成功");
                text(ref stu, ref student);
            }
        }
    }
}
